package com.killer.listener;


import com.killer.pojo.Order;
import com.killer.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class OrderListener {

    @Autowired
    private OrderService orderService;

    @RabbitListener(queues = "queue.dl")
    public void cancelExpireOrder(Message message, Channel channel) throws IOException {
        System.out.println("收到消息,deliveryTag:" + message.getMessageProperties().getDeliveryTag());
        String payLoad = new String(message.getBody(), message.getMessageProperties().getContentEncoding());
        Long orderId = Long.parseLong(payLoad);
        Order order = orderService.findOrderById(orderId);
        if (order.getStatus() == 0) {
            order.setStatus(-1);
            orderService.updateOrder(order);
            System.out.println("订单id：" + payLoad + "因过期未支付而被取消");
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
